using B.S.RbacData.Infrastructure;
using MediatR;
using SupplierCustomerManagement.Domain.Entities;
using B.S.RbacData.ErrorCode;
using SupplierCustomerManagement.Write.API.Applications.Commands.servicepersonals;

namespace SupplierCustomerManagement.Write.API.Applications.CommandHandlters.servicepersonals
{
    /// <summary>
    /// 删除服务人员命令处理器
    /// </summary>
    public class DeleteServicePersonalCommandHandler : IRequestHandler<DeleteServicePersonalCommand, ApiResult<bool>>
    {
        private readonly IBaseRepository<ServicePersonnel> _servicePersonalRepository;
        private readonly ILogger<DeleteServicePersonalCommandHandler> _logger;

        public DeleteServicePersonalCommandHandler(
            IBaseRepository<ServicePersonnel> servicePersonalRepository,
            ILogger<DeleteServicePersonalCommandHandler> logger)
        {
            _servicePersonalRepository = servicePersonalRepository;
            _logger = logger;
        }

        /// <summary>
        /// 处理删除服务人员命令
        /// </summary>
        /// <param name="request">删除服务人员命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>删除结果</returns>
        public async Task<ApiResult<bool>> Handle(DeleteServicePersonalCommand request, CancellationToken cancellationToken)
        {
            try
            {
                _logger.LogInformation("开始删除服务人员，服务人员ID: {ServicePersonalId}", request.Id);

                // 查找要删除的服务人员
                var servicePersonal = _servicePersonalRepository.GetAll().FirstOrDefault(x => x.Id == request.Id);

                if (servicePersonal == null || servicePersonal.IsDeleted)
                {
                    _logger.LogWarning("未找到指定的服务人员，服务人员ID: {ServicePersonalId}", request.Id);
                    
                    return new ApiResult<bool>
                    {
                        code = ResultEnumCode.fails,
                        message = "未找到指定的服务人员",
                        data = false
                    };
                }

                // 软删除服务人员
                servicePersonal.IsDeleted = true;
                servicePersonal.UpdatedTime = DateTime.Now;
                servicePersonal.UpdatedBy = 1; // 后续可以从当前用户上下文获取

                await _servicePersonalRepository.UpdateAsync(servicePersonal);

                _logger.LogInformation("成功删除服务人员，服务人员ID: {ServicePersonalId}, 服务人员姓名: {Name}", 
                    request.Id, servicePersonal.Name);

                return new ApiResult<bool>
                {
                    code = ResultEnumCode.Ok,
                    message = "删除服务人员成功",
                    data = true
                };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除服务人员时发生异常，服务人员ID: {ServicePersonalId}", request.Id);
                
                return new ApiResult<bool>
                {
                    code = ResultEnumCode.fails,
                    message = $"删除服务人员时发生异常: {ex.Message}",
                    data = false
                };
            }
        }
    }
}